package com.ischoolbar.programmer.dao;

/**
 * @Author lin
 **/

import com.ischoolbar.programmer.model.Page;
import com.ischoolbar.programmer.model.Student;
import com.ischoolbar.programmer.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDao extends BaseDao {
    public StudentDao() {
    }

    public boolean addStudent(Student student) {
        String sql = "insert into s_student values(null,'" + student.getSn() + "','" + student.getName() + "'";
        sql = sql + ",'" + student.getPassword() + "'," + student.getClazzId();
        sql = sql + ",'" + student.getSex() + "','" + student.getMobile() + "'";
        sql = sql + ",'" + student.getQq() + "',null)";
        return this.update(sql);
    }

    public boolean editStudent(Student student) {
        String sql = "update s_student set name = '" + student.getName() + "'";
        sql = sql + ",sex = '" + student.getSex() + "'";
        sql = sql + ",mobile = '" + student.getMobile() + "'";
        sql = sql + ",qq = '" + student.getQq() + "'";
        sql = sql + ",clazz_id = " + student.getClazzId();
        sql = sql + " where id = " + student.getId();
        return this.update(sql);
    }

    public boolean setStudentPhoto(Student student) {
        String sql = "update s_student set photo = ? where id = ?";
        Connection connection = this.getConnection();
        try {
            PreparedStatement prepareStatement = connection.prepareStatement(sql);
            prepareStatement.setBinaryStream(1, student.getPhoto());
            prepareStatement.setInt(2, student.getId());
            return prepareStatement.executeUpdate() > 0;
        } catch (SQLException var5) {
            var5.printStackTrace();
            return this.update(sql);
        }
    }

    public boolean deleteStudent(String ids) {
        String sql = "delete from s_student where id in(" + ids + ")";
        return this.update(sql);
    }

    public Student getStudent(int id) {
        String sql = "select * from s_student where id = " + id;
        Student student = null;
        ResultSet resultSet = this.query(sql);
        try {
            if (resultSet.next()) {
                student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setClazzId(resultSet.getInt("clazz_id"));
                student.setMobile(resultSet.getString("mobile"));
                student.setName(resultSet.getString("name"));
                student.setPassword(resultSet.getString("password"));
                student.setPhoto(resultSet.getBinaryStream("photo"));
                student.setQq(resultSet.getString("qq"));
                student.setSex(resultSet.getString("sex"));
                student.setSn(resultSet.getString("sn"));
                return student;
            }
        } catch (SQLException var6) {
            var6.printStackTrace();
        }
        return student;
    }

    public List<Student> getStudentList(Student student, Page page) {
        List<Student> ret = new ArrayList();
        String sql = "select * from s_student ";
        if (!StringUtil.isEmpty(student.getName())) {
            sql = sql + "and name like '%" + student.getName() + "%'";
        }
        if (student.getClazzId() != 0) {
            sql = sql + " and clazz_id = " + student.getClazzId();
        }
        if (student.getId() != 0) {
            sql = sql + " and id = " + student.getId();
        }
        sql = sql + " limit " + page.getStart() + "," + page.getPageSize();
        ResultSet resultSet = this.query(sql.replaceFirst("and", "where"));

        try {
            while (resultSet.next()) {
                Student s = new Student();
                s.setId(resultSet.getInt("id"));
                s.setClazzId(resultSet.getInt("clazz_id"));
                s.setMobile(resultSet.getString("mobile"));
                s.setName(resultSet.getString("name"));
                s.setPassword(resultSet.getString("password"));
                s.setPhoto(resultSet.getBinaryStream("photo"));
                s.setQq(resultSet.getString("qq"));
                s.setSex(resultSet.getString("sex"));
                s.setSn(resultSet.getString("sn"));
                ret.add(s);
            }
        } catch (SQLException var7) {
            var7.printStackTrace();
        }
        return ret;
    }

    public int getStudentListTotal(Student student) {
        int total = 0;
        String sql = "select count(*) as total from s_student ";
        if (!StringUtil.isEmpty(student.getName())) {
            sql = sql + "and name like '%" + student.getName() + "%'";
        }
        if (student.getClazzId() != 0) {
            sql = sql + " and clazz_id = " + student.getClazzId();
        }
        if (student.getId() != 0) {
            sql = sql + " and id = " + student.getId();
        }
        ResultSet resultSet = this.query(sql.replaceFirst("and", "where"));
        try {
            while (resultSet.next()) {
                total = resultSet.getInt("total");
            }
        } catch (SQLException var6) {
            var6.printStackTrace();
        }
        return total;
    }

    public Student login(String name, String password) {
        String sql = "select * from s_student where name = '" + name + "' and password = '" + password + "'";
        ResultSet resultSet = this.query(sql);
        try {
            if (resultSet.next()) {
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setPassword(resultSet.getString("password"));
                student.setClazzId(resultSet.getInt("clazz_id"));
                student.setMobile(resultSet.getString("mobile"));
                student.setPhoto(resultSet.getBinaryStream("photo"));
                student.setQq(resultSet.getString("qq"));
                student.setSex(resultSet.getString("sex"));
                student.setSn(resultSet.getString("sn"));
                return student;
            }
        } catch (SQLException var6) {
            var6.printStackTrace();
        }
        return null;
    }

    public boolean editPassword(Student student, String newPassword) {
        String sql = "update s_student set password = '" + newPassword + "' where id = " + student.getId();
        return this.update(sql);
    }
}
